package com.disruptor.sample._02producer.procucer;

import com.disruptor.sample.common.dto.LongEvent;
import com.lmax.disruptor.RingBuffer;
import java.nio.ByteBuffer;

public class LongEventProducer {

  private final RingBuffer<LongEvent> ringBuffer;

  public LongEventProducer(RingBuffer<LongEvent> ringBuffer) {
    this.ringBuffer = ringBuffer;
  }

  public void onData(ByteBuffer byteBuffer) {
    // 1 	获取下一个序列
    long sequence = ringBuffer.next();
    try {
      // 2 获取 Disruptor 中该序列的条目
      LongEvent event = ringBuffer.get(sequence);

      // 3 用数据填充条目
      event.setValue(byteBuffer.getLong(0));
    } finally {
      ringBuffer.publish(sequence);
    }
  }
}
